package com.riant.mybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.riant.mybatis.bean.WebSite;
import com.riant.mybatis.service.WebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname WebSiteController
 * @Description TODO
 * @Date 2021/6/3 22:22
 * @Created by 张斌
 */
@RestController
@RequestMapping("/website")
public class WebSiteController {

    @Autowired
    private WebSiteService webSiteService;

    @RequestMapping(path = "/v1/{id}", method = RequestMethod.GET)
    public WebSite getWebSite(@PathVariable int id) {
        return webSiteService.getWebSite(id);
    }

    @RequestMapping(path = "/v1/website", method = RequestMethod.GET)
    public List<WebSite> getAllWebSite() {
        List<WebSite> list = webSiteService.getAllWebSite();
        return webSiteService.getAllWebSite();
    }

    @RequestMapping(path = "/v1/{pageNum}/{size}", method = RequestMethod.GET)
    public List<WebSite> getAllWebSite(@PathVariable int pageNum, @PathVariable int size) {
        List<WebSite> list = webSiteService.getAllWebSite(pageNum, size);
        return list;
    }

    @RequestMapping(path = "/v1/update", method = RequestMethod.POST)
    public JSONObject updateWebSite(@RequestBody WebSite webSite) {
        Integer result = webSiteService.updateWebSite(webSite);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("code", "000000");
            jsonObject.put("message", "SUCCESS");
        } else {
            jsonObject.put("code", "999999");
            jsonObject.put("message", "FAIL");
        }
        List<WebSite> list = webSiteService.getAllWebSite();
        return jsonObject;
    }

    @RequestMapping(path = "/v1/insert", method = RequestMethod.POST)
    public JSONObject insertWebSite(@RequestBody WebSite webSite) {
        Integer result = webSiteService.insertWebSite(webSite);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("code", "000000");
            jsonObject.put("message", "SUCCESS");
        } else {
            jsonObject.put("code", "999999");
            jsonObject.put("message", "FAIL");
        }
        return jsonObject;
    }
    @RequestMapping(path = "/v1/{id}", method = RequestMethod.DELETE)
    public JSONObject deleteWebSite(@PathVariable int id) {
        Integer result = webSiteService.deleteWebSite(id);
        JSONObject jsonObject = new JSONObject();
        if (result > 0) {
            jsonObject.put("code", "000000");
            jsonObject.put("message", "SUCCESS");
        } else {
            jsonObject.put("code", "999999");
            jsonObject.put("message", "FAIL");
        }
        return jsonObject;
    }
}
